package cube.utils.msg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.TreeMap;

import cube.utils.BaseUtil;

/**
 * 资源文件读取工具
 * 
 * @author abiao
 */
public class MsgUtil {

    /**
     * 资源绑定
     */
    private ResourceBundle resBundle;

    /**
     * 资源绑定文件
     */
    private String bundle;

    /**
     * 键名缓存池
     */
    private Map<String, List<String>> keysPool;

    /**
     * 名称-值缓存池
     */
    private Map<String, Map<String, LabelValue>> lvPool;

    /**
     * 资源缓存池
     */
    private Map<String, String> resPool = new HashMap<String, String>();

    /**
     * 资源获取工具
     */
    public MsgUtil(String bundle) {
        this.bundle = bundle;
        this.resBundle = ResourceBundle.getBundle(bundle);
        keysPool = new HashMap<String, List<String>>();
        lvPool = new HashMap<String, Map<String, LabelValue>>();
    }

    /**
     * 根据key获取value
     */
    public String get(String key) {
        String msg = null;
        try {
            msg = resBundle.getString(key);
        } catch (MissingResourceException e) {
            BaseUtil.log.error("在资源文件" + bundle + ".properties中找不到" + key
                    + "属性!");
        }
        return msg;
    }

    /**
     * 获取带参数的信息
     */
    public String get(String key, Object... args) {
        String msg = get(key);
        for (int i = 0; i < args.length; i++) {
            msg = msg.replace("{" + i + "}", args[i].toString());
        }
        return msg;
    }

    /**
     * 根据key获取value
     */
    public byte getByte(String key) {
        return Byte.parseByte(get(key));
    }

    /**
     * 根据key获取value
     */
    public short getShort(String key) {
        return Short.parseShort(get(key));
    }

    /**
     * 根据key获取value
     */
    public int getInt(String key) {
        return Integer.parseInt(get(key));
    }

    /**
     * 根据key获取长整形value
     */
    public long getLong(String key) {
        return Long.parseLong(get(key));
    }

    /**
     * 根据key获取长整形value
     */
    public double getDouble(String key) {
        return Double.parseDouble(get(key));
    }

    /**
     * 根据key获取长整形value
     */
    public float getFloat(String key) {
        return Float.parseFloat(get(key));
    }

    /**
     * 根据key获取布尔value
     */
    public boolean getBool(String key) {
        return Boolean.parseBoolean(get(key));
    }

    /**
     * 通过前缀查找键的集合
     */
    public List<String> findKeys(String prefix) {
        if (keysPool.containsKey(prefix)) {
            return keysPool.get(prefix);
        } else {
            return findKeysFromRes(prefix);
        }
    }

    /**
     * 按照前缀从资源文件中寻找keys
     */
    private List<String> findKeysFromRes(String prefix) {
        List<String> prefixKeys = new ArrayList<String>();
        for (String key : Collections.list(resBundle.getKeys())) {
            if (key.startsWith(prefix)) {
                prefixKeys.add(key);
            }
        }
        if (!prefixKeys.isEmpty()) {
            keysPool.put(prefix, prefixKeys);
        }
        return prefixKeys;
    }

    /**
     * 获取所有资源
     */
    public Map<String, String> getAll() {
        if (resPool.isEmpty()) {
            for (String key : Collections.list(resBundle.getKeys())) {
                resPool.put(key, get(key));
            }
        }
        return resPool;
    }

    /**
     * 通过值查找名称
     */
    public String getLabel(String prefix, Object value) {
        if (!lvPool.containsKey(prefix)) {
            lvPool.put(prefix, getLabelValueMap(prefix));
        }
        Map<String, LabelValue> lvs = lvPool.get(prefix);
        String v = value.toString();
        return !lvs.containsKey(v) ? "" : lvs.get(v).getLabel();
    }

    /**
     * 获取名称-值集合
     */
    public Map<String, LabelValue> getLabelValueMap(String prefix) {
        Map<String, LabelValue> labelValueMap = new TreeMap<String, LabelValue>();
        List<String> valueKeys = findKeys(prefix + ".value.");
        for (String valueKey : valueKeys) {
            String label = getLabelFromRes(prefix, valueKey);
            String value = getValueFromRes(valueKey);
            labelValueMap.put(value, new LabelValue(label, value));
        }
        return labelValueMap;
    }

    /**
     * 获取值
     */
    private String getValueFromRes(String valueKey) {
        return get(valueKey);
    }

    /**
     * 获取名称
     */
    private String getLabelFromRes(String prefix, String vk) {
        return get(vk.replaceFirst(prefix + ".value.", prefix + ".name."));
    }
}
